import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-todolist',
  templateUrl: './todolist.component.html',
  styleUrls: ['./todolist.component.scss']
})
export class TodolistComponent implements OnInit {
  public keywords: string
  public todoList: any[] = [
    {
      title: '开会',
      status: 0
    },
    {
      title: '午休',
      status: 1
    }
  ]
  constructor() { }

  ngOnInit(): void {
  }
  doAdd(e) {
    if (e.keyCode === 13) {
      // 0代表待办事项，1表示已完成事项
      if (!this._todolistHasKeyword(this.todoList, this.keywords)) {
        this.todoList.push({
          title: this.keywords,
          status: 0
        })
        this.keywords = ''
      } else {
        alert('重复输入')
        this.keywords = ''
      }
    }
  }
  deleteData(key) {
    this.todoList.splice(key, 1)
  }

  _todolistHasKeyword(todolist: any, keywords: any) {
    return todolist.some(todo => todo.title === keywords)
  }
}